//
//  CoffeeArtViewController.m
//  Fragrance
//
//  Created by dllo on 15/5/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "CoffeeArtViewController.h"
#import <UIImageView+AFNetworking.h>

@interface CoffeeArtViewController ()

@end

@implementation CoffeeArtViewController

- (void)viewDidLoad {
    [super viewDidLoad];

self.navigationItem.title = @"咖啡文化";
    
    activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    activity.frame = CGRectMake(0, 0, 40, 40);
    activity.color = [UIColor brownColor];
    [self.view addSubview:activity];
    activity.center = self.view.center;
    [activity startAnimating];
    [activity release];

//提示的同时加载数据



    [self downLoadThisPage];











}



- (void)initTableView
{
    CoffeeTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    CoffeeTableView.delegate = self;
    CoffeeTableView.dataSource = self;
    [self.view addSubview:CoffeeTableView];
    [CoffeeTableView release];
}

-(void)downLoadThisPage
{
    NSString *str = @"http://www.baike.com/api.php?m=article&datatype=json&a=new&baike_id=1073&start=0&limit=10";
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:str];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
    request.HTTPMethod = @"GET";
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
       
        [activity stopAnimating];
        //数据请求完成，开始解析
        self.arrayData = [NSMutableArray array];
        self.arrayData = [CoffeeJX getCoffeeModel:data];
        
        //数据源准备完成，可以添加tableview
        [self initTableView];
        
    }];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifer = @"cell";
    CoffeeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
    if (cell == nil) {
        cell = [[CoffeeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
    }
    
    CoffeeModel *model = [self.arrayData objectAtIndex:indexPath.row];
    
    
    cell.titleLabel.text = model.title;
    cell.cultureLabel.text = model.culture;
//    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:model.pic]];
//    UIImage *image = [UIImage imageWithData:data];
//    cell.coffeeIcon.image = image;
    NSLog(@"%@", model.pic);
    [cell.coffeeIcon setImageWithURL:[NSURL URLWithString:model.pic] placeholderImage:nil];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrayData.count;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 90;
    
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
